﻿using System;
using System.Collections;

namespace DataModel
{
    #region 图片数据
   public class Multimedia
    {
        public string StrLoc = "";
        //private bool FReceiveOver;
        public string DeviceID;
        public int TotalPacket;
        public int CurPacket;
        public string PhotoTime;
        public Hashtable MediaTable = new Hashtable();
        private string errCode = string.Empty;

        public string ErrCode
        {
            get { return errCode; }
            set { errCode = value; }
        }
        private string multiMediaID;
        //多媒体ID DWORD  >0
        public string MultiMediaID
        {
            get { return multiMediaID; }
            set { multiMediaID = value; }
        }

        private int multiMediaType;
        //多媒体类型  0：图像；1：音频；2：视频
        public int MultiMediaType
        {
            get { return multiMediaType; }
            set { multiMediaType = value; }
        }

        private int multiMediaFormat;
        //0：JPEG；1：TIF；2：MP3；3：WAV；4：WMV 其他保留
        public int MultiMediaFormat
        {
            get { return multiMediaFormat; }
            set { multiMediaFormat = value; }
        }

        private int multiMediaEvent;
        //事件项编码  0：平台下发指令；1：定时动作；2：抢劫报警触发；3：碰撞侧翻报警触发；4：开门触发；
        //5：自定义高触发；6：自定义低触发 其他保留
        public int MultiMediaEvent
        {
            get { return multiMediaEvent; }
            set { multiMediaEvent = value; }
        }

        private int channelID;
        //通道ID
        public int ChannelID
        {
            get { return channelID; }
            set { channelID = value; }
        }

        public string Data;

        public bool GetReceiveOver()
        {
            //string s1, s2;

            for (int i = 1; i <= this.TotalPacket; i++)
            {
                if (!MediaTable.ContainsKey(i))
                    return false;
                /*
                s1 = "[" + i.ToString() + ",";
                s2 = "," + i.ToString() + "]";
                if (this.Data.IndexOf(s1) < 0 || this.Data.IndexOf(s2) < 0)
                {
                    return false;
                }
                */
            }
            return true;
        }

        public void SaveErrID(int CurBack)
        {
            string tmp = string.Empty;
            int count = errCode.Length / 4;
            for (int i = 0; i < count; i++)
            {
                tmp = errCode.Substring(i * 4, 4);
                if (tmp == IntToHex(CurBack, 4))
                    return;
            }
            errCode = errCode + IntToHex(CurBack, 4);
        }


        #region 十进制转十六进制
        private string IntToHex(int n, int len)
        {
            string ret = Convert.ToString(n, 16);
            while (ret.Length < len)
            {
                ret = "0" + ret;
            }
            return ret;
        }
        #endregion

        public void CancelErrID(int CurBack)
        {
            int count = errCode.Length / 4;
            string NewCode = string.Empty;
            string tmp = string.Empty;
            for (int i = 0; i < count; i++)
            {
                tmp = errCode.Substring(i * 4, 4);
                if (tmp != IntToHex(CurBack, 4))
                {
                    NewCode = NewCode + tmp;
                }
            }
            errCode = NewCode;
        }

        public void AddImageData(int CurPacket, string Data)
        {
            if (MediaTable.ContainsKey(CurPacket))
                MediaTable[CurPacket] = Data;
            else
                MediaTable.Add(CurPacket, Data);
            /*
            string fg1, fg2, s1, s2;
            fg1 = string.Format("[{0},", CurPacket);
            fg2 = string.Format(",{0}]", CurPacket);
            int p1 = -1, p2 = -1;
            p1 = this.Data.IndexOf(fg1);
            p2 = this.Data.IndexOf(fg2);
            if (p1 >= 0 && p2 >= 0)
            {
                s1 = this.Data.Substring(0, p1 + fg1.Length);
                s2 = this.Data.Substring(p2);
                this.Data = s1 + Data + s2;
            }
            else
                this.Data = this.Data + fg1 + Data + fg2;
             */
        }

        public Multimedia(string DeviceID, int TotalPacket, int CurPacket, string PhotoTime)
        {
            this.DeviceID = DeviceID;
            this.TotalPacket = TotalPacket;
            this.CurPacket = CurPacket;
            this.PhotoTime = PhotoTime;
            this.Data = string.Empty;
        }

    }
    #endregion
}